package nexbit.icons.moonshine;

import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.ListView;
import android.graphics.Typeface;

import android.support.v4.widget.DrawerLayout;
import android.support.v4.app.Fragment;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends ActionBarActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private Toolbar mToolbar;
    private ListView navList;

    private Resources mResources;

    private String[] navTitles;
    private TypedArray navImages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeCommon();
        initializeDrawer();
        initializeToolbar();
        setFonts();
    }

    private void initializeCommon(){
        mResources = this.getResources();
    }

    private void initializeDrawer(){
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // pull in strings and images
        navTitles = mResources.getStringArray(R.array.nav_list_titles);
        navImages = mResources.obtainTypedArray(R.array.nav_list_images);

        // Listed Items
        NavListItem adapter = new NavListItem(MainActivity.this, navTitles, navImages);
        navList = (ListView) findViewById(R.id.nav_list);
        navList.setAdapter(adapter);
        handleNavListClick();
    }

    private void handleNavListClick(){
        navList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                navList.setItemChecked(position,true);
            }
        });
    }

    private void initializeToolbar(){
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_menu_wht_24dp);

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                mToolbar,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_open  /* "close drawer" description */
        ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                //getActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                //getActionBar().setTitle(mDrawerTitle);
            }
        };
    }

    private void setFonts(){
        TextView drawerTitle = (TextView) findViewById(R.id.drawer_title);
        TextView drawerSubtitle = (TextView) findViewById(R.id.drawer_subtitle);

        Typeface rMedium = Typeface.createFromAsset(getAssets(), "Roboto-Medium.ttf");
        Typeface rRegular = Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf");

        drawerTitle.setTypeface(rMedium);
        drawerSubtitle.setTypeface(rRegular);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}
